package com.jinlia.show.modules.system.controller.sys;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinlia.show.modules.base.system.entity.SysDict;
import com.jinlia.show.modules.base.model.po.SysDictPO;
import com.jinlia.show.modules.base.model.po.SysDictTypePO;
import com.jinlia.show.modules.system.model.Option;
import com.jinlia.show.modules.base.model.form.DictForm;
import com.jinlia.show.modules.base.model.form.DictTypeForm;
import com.jinlia.show.modules.base.model.query.DictPageQuery;
import com.jinlia.show.modules.base.model.query.DictTypePageQuery;
import com.jinlia.show.modules.system.service.SysDictService;
import com.jinlia.show.modules.system.service.SysDictTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("dict")
public class SysDictController {
    private final SysDictService dictService;

    private final SysDictTypeService dictTypeService;

    //(summary = "字典分页列表")
    @PostMapping("/page")
    public ResponseEntity<Page<SysDict>> getDictPage(
            @RequestBody DictPageQuery queryParams
    ) {
        Page<SysDict> dictPage = dictService.getDictPage(queryParams);
        return ResponseEntity.ok(dictPage);
    }

    //(summary = "字典数据表单数据")
    @GetMapping("/vo/{id}")
    public ResponseEntity<SysDictPO> getDictForm(@PathVariable Long id
    ) {
        SysDictPO formData = dictService.getDictForm(id);
        return ResponseEntity.ok(formData);
    }

    //(summary = "新增字典")
    @PostMapping("add")
    public ResponseEntity saveDict(
            @RequestBody DictForm DictForm
    ) {
        boolean result = dictService.saveDict(DictForm);
        return ResponseEntity.ok(result);
    }

    //(summary = "修改字典")
    @PutMapping("/update/{id}")
    public ResponseEntity updateDict(
            @PathVariable Long id,
            @RequestBody DictForm DictForm
    ) {
        boolean status = dictService.updateDict(id, DictForm);
        return ResponseEntity.ok(status);
    }

    //(summary = "删除字典")
    @DeleteMapping("/del/{ids}")
    //("@ss.hasPerm('sys:dict:delete')")
    public ResponseEntity deleteDict(@PathVariable String ids
    ) {
        boolean result = dictService.deleteDict(ids);
        return ResponseEntity.ok(result);
    }


    //(summary = "字典下拉列表")
    @GetMapping("/{typeCode}/options")
    public ResponseEntity<List<Option>> listDictOptions(@PathVariable String typeCode
    ) {
        List<Option> list = dictService.listDictOptions(typeCode);
        return ResponseEntity.ok(list);
    }


    /*----------------------------------------------------*/
    //(summary = "字典类型分页列表")
    @PostMapping("/types/page")
    public ResponseEntity<Page<SysDictTypePO>> getDictTypePage(@RequestBody DictTypePageQuery queryParams
    ) {
        Page<SysDictTypePO> result = dictTypeService.getDictTypePage(queryParams);
        return ResponseEntity.ok(result);
    }

    //(summary = "字典类型表单数据")
    @GetMapping("/types/vo/{id}")
    public ResponseEntity<SysDictTypePO> getDictTypeForm(@PathVariable Long id
    ) {
        SysDictTypePO dictTypeForm = dictTypeService.getDictTypeForm(id);
        return ResponseEntity.ok(dictTypeForm);
    }

    //(summary = "新增字典类型")
    @PostMapping("/types/add")
    //("@ss.hasPerm('sys:dict_type:add')")
    public ResponseEntity saveDictType(@RequestBody SysDictTypePO dictTypeForm) {
        boolean result = dictTypeService.saveDictType(dictTypeForm);
        return ResponseEntity.ok(result);
    }

    //(summary = "修改字典类型")
    @PutMapping("/types/update/{id}")
    //("@ss.hasPerm('sys:dict_type:edit')")
    public ResponseEntity updateDictType(@PathVariable Long id, @RequestBody DictTypeForm dictTypeForm) {
        boolean status = dictTypeService.updateDictType(id, dictTypeForm);
        return ResponseEntity.ok(status);
    }

    //(summary = "删除字典类型")
    @DeleteMapping("/types/{ids}")
    public ResponseEntity deleteDictTypes(@PathVariable String ids
    ) {
        boolean result = dictTypeService.deleteDictTypes(ids);


        return ResponseEntity.ok(result);
    }

}
